package com.qzdsoft.erpcloud.service.pu.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.qzdsoft.erpcloud.domain.pu.PurchaseInquiry;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseInquiryDetailMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseInquiryMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseInquiryQueryMapper;
import com.qzdsoft.erpcloud.service.pu.PurchaseInquiryService;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryQueryInfo;
import com.qzdsoft.utils.DateUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

@Service
public class PurchaseInquiryServiceImpl implements PurchaseInquiryService {
	
    private static final Logger logger = LoggerFactory.getLogger(PurchaseOrderServiceImpl.class);
    
	@Autowired
	private PurchaseInquiryQueryMapper purchaseInquiryQueryMapper;
	
	@Autowired
	private PurchaseInquiryMapper purchaseInquiryMapper;
	
	@Autowired
	private PurchaseInquiryDetailMapper purchaseInquiryDetailMapper;
	
	@Override
	public LayTableResponse<PurchaseInquiryInfo> findPurchaseInquirysBy(PurchaseInquiryQueryInfo info) {
	    logger.debug("查询采购询价单，参数：{}",info);
	    //指定时间+1天
        if(!StringUtil.isEmpty(info.getEndDate())) {
            info.setEndDate(DateUtil.getSpecifiedDayAfter(info.getEndDate()));
        }
	    PageHelper.startPage(info.getPage(), info.getLimit());
		List<PurchaseInquiryInfo> purchaseInquirys = purchaseInquiryQueryMapper.selectAll(info);
		LayTableResponse<PurchaseInquiryInfo> po = new LayTableResponse<PurchaseInquiryInfo>(purchaseInquirys);
		return po;
	}
	
	@Override
	public PurchaseInquiryInfo findPurchaseInquiryBy(Integer id) {
		return purchaseInquiryQueryMapper.selectOneBy(id);
	}
	
	@Transactional
    @Override
    public Response<String> add(PurchaseInquiryEditInfo info){
        PurchaseInquiry purchaseInquiry = new PurchaseInquiry();
        purchaseInquiry.setCompanyId(info.getCompanyid());
        purchaseInquiry.setBillNo(info.getBillno());
        purchaseInquiry.setSupplierId(info.getSupplierId());
        purchaseInquiry.setSupplierContactId(info.getConnactId());
        purchaseInquiry.setStatementId(info.getSettleMethodId());
        purchaseInquiry.setStatementName(info.getSettleMethodName());
        purchaseInquiry.setStatementDays(info.getSettleDays());
        purchaseInquiry.setStatementCurrencyId(info.getCurrencyId());
        purchaseInquiry.setStatementExchangeRate(Integer.parseInt(info.getExchangeRate()));
        purchaseInquiry.setStatementTaxRate(Integer.parseInt(info.getTaxRate()));
        purchaseInquiry.setCreatorId(info.getCreatorId());
        //purchaseInquiry.setCreatorId(info.getUserid());
        purchaseInquiry.setCtime(new Date());
        purchaseInquiry.setRemark(info.getRemark());
        purchaseInquiry.setReviewStatus(Integer.parseInt(PurchaseOrderStatus.NOT_REVIEW_CODE));
        int i = purchaseInquiryMapper.insert(purchaseInquiry);
        if(i>0){
            return new Response<>(ResultEnum.SUCCESS);
        }else{
            return new Response<>(ResultEnum.ERROR);
        }
	}
    
    @Transactional
    @Override
    public Response<String> remove(Integer id){
        int delInquiryCount = purchaseInquiryMapper.deleteByPrimaryKey(id);
        if(delInquiryCount!=1){
            logger.error("删除采购询价单，根据id:{}查询到的采购询价单为空",id);
            throw new RuntimeException("根据id查询到的采购询价单为空");
        }
        int delDetailCount = purchaseInquiryDetailMapper.deleteByBillId(id);
        logger.debug("根据billid：{}删除采购询价单明细行数：{}",id,delDetailCount);
        return new Response<String>(ResultEnum.SUCCESS);
    }

	@Transactional
	@Override
	public Response<String> edit(PurchaseInquiryEditInfo info) {
	    PurchaseInquiry purchaseInquiry = purchaseInquiryMapper.selectByPrimaryKey(info.getId());
        if(purchaseInquiry==null) {
            logger.error("编辑采购询价单，根据id:{}查询到的采购询价单为空",info.getId());
            throw new RuntimeException("根据id查询到的采购询价单为空") ;
        }
        purchaseInquiry.setCompanyId(info.getCompanyid());
        purchaseInquiry.setSupplierId(info.getSupplierId());
        purchaseInquiry.setSupplierContactId(info.getConnactId());
        purchaseInquiry.setStatementCurrencyId(info.getCurrencyId());
        purchaseInquiry.setStatementDays(info.getSettleDays());
        purchaseInquiry.setStatementExchangeRate(Integer.parseInt(info.getExchangeRate()));
        purchaseInquiry.setStatementId(info.getSettleMethodId());
        purchaseInquiry.setStatementTaxRate(Integer.parseInt(info.getTaxRate()));
        purchaseInquiry.setStatementName(info.getSettleMethodName());
        purchaseInquiry.setModifyTime(new Date());
        purchaseInquiry.setModifyId(info.getCreatorId());
        //purchaseInquiry.setModifyId(info.getUserid());
        purchaseInquiryMapper.updateByPrimaryKey(purchaseInquiry);
		return new Response<String>(ResultEnum.SUCCESS);
	}

	@Transactional
    @Override
    public Response<PurchaseInquiryInfo> reivew(Integer userid,Integer id){
	    PurchaseInquiry record = purchaseInquiryMapper.selectByPrimaryKey(id);
        if(record==null) {
            logger.error("审核采购询价单，根据id:{}查询到的采购询价单为空",id);
            throw new RuntimeException("根据id查询到的采购询价单为空") ;
        }
        record.setReviewId(userid);
        record.setReviewTime(new Date());
        record.setReviewStatus(Integer.parseInt(PurchaseOrderStatus.HAS_REVIEW_CODE));
        int count = purchaseInquiryMapper.updateByPrimaryKey(record);
        if(count==1) {
            PurchaseInquiryInfo purchaseInquiryInfo = purchaseInquiryQueryMapper.selectOneBy(id);
            return new Response<PurchaseInquiryInfo>(ResultEnum.SUCCESS,purchaseInquiryInfo);
        }else {
            return new Response<PurchaseInquiryInfo>(ResultEnum.ERROR);
        }
    }
	
	@Transactional
    @Override
    public Response<PurchaseInquiryInfo> cancelReview(Integer userid,Integer id){
	    PurchaseInquiry record = purchaseInquiryMapper.selectByPrimaryKey(id);
	    if(record==null){
	        logger.error("弃审采购询价单，根据id:{}查询到的采购询价为空", id);
	        throw new RuntimeException("根据id查询到的采购询价单为空");
	    }
	    record.setModifyId(userid);
	    record.setReviewTime(new Date());
	    record.setReviewStatus(Integer.parseInt(PurchaseOrderStatus.NOT_REVIEW_CODE));
	    int count = purchaseInquiryMapper.updateByPrimaryKey(record);
	    if(count==1){
	        PurchaseInquiryInfo purchaseInquiryInfo = purchaseInquiryQueryMapper.selectOneBy(id);
	        return new Response<PurchaseInquiryInfo>(ResultEnum.SUCCESS,purchaseInquiryInfo);
	    }else{
	        return new Response<PurchaseInquiryInfo>(ResultEnum.ERROR);
	    }
    }

	@Transactional
    @Override
    public Response<String> close(Integer id){
//        PurchaseInquiry record = purchaseInquiryMapper.selectByPrimaryKey(id);
//        if(record == null){
//            logger.error("关闭采购询价单，根据id:{}查询到的采购询价单为空",id);
//            throw new RuntimeException("根据id查询到的采购询价单为空");
//        }
//        record.setReviewTime(new Date());
//        record.setReviewStatus(Integer.parseInt(PurchaseOrderStatus.CLOSE_CODE));
//        int count = purchaseInquiryMapper.updateByPrimaryKey(record);
//        if(count==1) {
//            return new Response<String>(ResultEnum.SUCCESS);
//        }else {
//            return new Response<String>(ResultEnum.ERROR);
//        }
        return null;
    }

    @Override
    public List<PurchaseInquiry> selectReviewOrderByCompanyId(PurchaseInquiry record){
        return purchaseInquiryMapper.select(record);
    }

}
